// view/index/fadt/fadt.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    buttonClicked:false,
    yuming: yuming,
    heights: "",
    title: '发布动态',
    photo: [],
    wuphoto: '',
    content: '',
    avatar: '',
    nickname: '',
    scnum: 9
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    console.log(dd)
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  // 放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 多组放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    var arr = []
    for (let i in list) {
      arr.push(list[i]); //属性
    }
    wx.previewImage({
      current: img,
      urls: arr
    })
  },
  // 上传
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.wuduoupimg(tempFilePaths[i]).then(res => {
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  nei(e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 敏感词过滤
  guolv() {
    let that = this;
    this.myRequest(yuming + 'Besall/mesg_sec_cheack', {
      content: that.data.content
    }, 'POST').then(res => {
      if (res.data.code == 200) {
        this.yanzheng(res.data.data)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  // 敏感词验证
  yanzheng(content) {
    this.myRequest(yuming + 'Besall/msg_sec_check', {
      from: 'EabEnd',
      content
    }, 'POST').then(res => {
      console.log(res);
      if (res.data.code == 200) {
        this.fabu(content);
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  // 发布
  fabu(content) {
    var that = this
    if (that.data.content == '' && that.data.wuphoto == '') {
      wx.showToast({
        title: '无内容',
        icon: 'none'
      })
    } else {
      that.setData({
        buttonClicked:true
      })
      this.myRequest(yuming + 'shop/shopAddBossCircle', {
        shopId: wx.getStorageSync('shopId'),
        content,
        images: that.data.wuphoto
      }, "POST").then(res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 0,
            })
          }, 1000)
        } else {
          that.setData({
            buttonClicked: false
          })
        }
      })
    }
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      nickname: options.nickname,
      avatar: options.avatar
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})